package com.mycompany.app.common.security.acegi.login;

import org.acegisecurity.Authentication;
import org.acegisecurity.event.authentication.AuthenticationFailureBadCredentialsEvent;
import org.acegisecurity.event.authentication.AuthenticationSuccessEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

/**
 * Acegi listener, 不足: 可获取的信息有限(ip,request url)
 * @author Shawn.guo
 *
 */
public class LoginListener implements ApplicationListener {

    public void onApplicationEvent(ApplicationEvent event) {

        if (event instanceof AuthenticationSuccessEvent) {
            AuthenticationSuccessEvent authEvent = (AuthenticationSuccessEvent) event;
            Authentication auth = authEvent.getAuthentication();
            String userName = auth.getName();
            System.out.println("#################mock user[" + userName
                    + "]logging success#################");
        }

        if (event instanceof AuthenticationFailureBadCredentialsEvent) {
            AuthenticationFailureBadCredentialsEvent authEvent = (AuthenticationFailureBadCredentialsEvent) event;
            Authentication auth = authEvent.getAuthentication();
            String userName = auth.getName();
            System.out
                    .println("#################mock user["
                            + userName
                            + "]logging failure[wrong user account or password]#################");
        }
    }

}
